<?php

class Inbound_InventoryController extends Zend_Controller_Action
{
	protected $_object;

	public function init()
	{
		//tạo đối tượng chính của module
		$this->_object = new Admin_Model_Inventory ();
		
		//lấy tiêu đề 
		$this->_helper->common->setCaptions ( array (), array ('inventory', 'product', 'location' ) );
	}

	//	xem danh sách tất cả - ho tro ajax 
	public function ajaxIndexAction()
	{
		//Dung layout ajax
		$this->getHelper ( 'Layout' )->setLayout ( 'ajax' );
		
		//lấy tham số
		$request = $this->_request;
		$inventory_status = $request->getParam ( 'inventory_status', '' );
		$inventory_product_code = $request->getParam ( 'inventory_product_code', '' );
		$inventory_lot_code = $request->getParam ( 'inventory_lot_code', '' );
		$product_warehouse_group_code = $request->getParam ( 'product_warehouse_group_code', '' );
		
		//các cột cần lấy dữ liệu
		$select = " 
		SELECT
			`inventory_id`
			, `inventory_product_code`
			, `inventory_product_unit_code`
			, `inventory_quantity`
			, `inventory_lot_code`
			, `inventory_product_time`
			, `inventory_expire_time`
			, `inventory_location_code`
			, `location_name`
		FROM tbl_inventory INNER JOIN tbl_location ON inventory_location_code = location_code
		INNER JOIN tbl_product ON (product_code = inventory_product_code AND product_warehouse_group_code='$product_warehouse_group_code')
		";
		
		//thiết lập điều kiện lấy dữ liệu
		$where = " WHERE 1 ";
		if ($inventory_product_code != '')
			$where .= " AND `inventory_product_code` = '$inventory_product_code' ";
		if ($inventory_lot_code != '')
			$where .= " AND `inventory_lot_code` = '$inventory_lot_code' ";
			
		//thiết lập thứ tự lấy dữ liệu
		$order = $this->_helper->common->getOrderString ( array ('inventory_id DESC' ) );
		
		//lấy dữ liệu
		$query = $select . $where . $order;
		$data = $this->_object->queryBase ( $query );
		
		//inventory_product_code AutoComplete control
		$inventory_product_code_control = new Itvina_JQuery_Form_Element_AutoComplete('inventory_product_code', 
			array( 'i_source' => $data, 'i_condition' => '$row->inventory_product_code')
		);
		$inventory_product_code_control->setValue( $inventory_product_code );
		
		//inventory_lot_code AutoComplete control
		$inventory_lot_code_control = new Itvina_JQuery_Form_Element_AutoComplete('inventory_lot_code', 
			array( 'i_source' => $data, 'i_condition' => '$row->inventory_lot_code')
		);
		$inventory_lot_code_control->setValue( $inventory_lot_code );
		
		//phân trang
		$this->_helper->common->paginate ( $data );
		
		//truyền dữ liệu ra view
		$this->view->inventory_status = $inventory_status;
		$this->view->inventory_product_code_control = $inventory_product_code_control;
		$this->view->inventory_lot_code_control = $inventory_lot_code_control;
		$this->view->data = $data;
	}
}